package jmotioncapture.testing.framework;

import jmotioncapture.camera.Frame;
import jmotioncapture.camera.FrameSet;
import jmotioncapture.geometry2D.Point;

import java.awt.Graphics;
import java.util.Vector;
import java.awt.geom.Line2D;
import java.awt.Graphics2D;
import javax.swing.JPanel;

public class DrawPanel extends JPanel {

	final static long serialVersionUID = 1;

	private FrameSet frames;

	private boolean withLinkedLine = false;

	private boolean withPoint = false;

	private boolean withLabel = false;


	public DrawPanel(FrameSet frames, boolean withLinkedLine,
			boolean withPoint, boolean withLabel) {
		this.frames = frames;
		this.withLinkedLine = withLinkedLine;
		this.withPoint = withPoint;
		this.withLabel = withLabel;
	}

	public void paintComponent(Graphics g) {
		// call paintComponent to ensure the panel displays correctly
		super.paintComponent(g);

		Graphics2D g2 = (Graphics2D) g;
		for (int j = 0; j < frames.size(); j++) {
			Frame frameGeo = frames.frameAt(j);
			Vector<Point> points = frameGeo.getPoints();

			for (int i = 0; i < points.size(); i++) {

				// Get point data
				double p1x = points.elementAt(i).getX();
				double p1y = points.elementAt(i).getY();

				double p2x = 0;
				double p2y = 0;
				if (i != points.size() - 1) {
					p2x = points.elementAt(i + 1).getX();
					p2y = points.elementAt(i + 1).getY();
				}

				// Draw point
				if (withPoint)
					g2.fillOval((int) p1x, (int) p1y, 5, 5);

				// Draw Label
				if (withLabel)
					g2.drawString("(" + p1x + "," + p1y + ")",
							((int) p1x + 10), (int) p1y);

				// Draw Line
				if (withLinkedLine && i != points.size() - 1) {
					g2.draw(new Line2D.Double(p1x, p1y, p2x, p2y));
				}
			}			
		}
	}
}
